package com.idol.framework.util;

import java.io.File;
import java.util.List;

/**
 * @author Supreme_Sir
 * @version 1.0
 * @className ScanUtil
 * @description 包扫描工具类
 * @date 2020/10/31 14:38
 **/
public class ScanUtil {
    public static List<String> doScan(String scanPackage, List<String> resultContainer) {
        // 获取被扫描包所在的绝对路径
        String packageAbsolutePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() +
                scanPackage.replaceAll("\\.", "/");
        File dirFile = new File(packageAbsolutePath);
        File[] subFiles = dirFile.listFiles();
        for (File file : subFiles) {
            // 如果当前文件仍为文件夹则进行递归查找
            if (file.isDirectory()) {
                doScan(scanPackage + "." + file.getName(), resultContainer);
            } else if (file.getName().endsWith(".class")) {
                String classPath = scanPackage + "." + file.getName().replace(".class", "");
                resultContainer.add(classPath);
            }
        }
        return resultContainer;
    }
}
